<?php

/**
 * This is the model class for table "hrm_user".
 *
 * The followings are the available columns in table 'hrm_user':
 * @property string $userId
 * @property string $name
 * @property string $password
 * @property string $email
 * @property integer $sex
 * @property string $dateOfBirth
 * @property string $address
 * @property integer $messageSubscribed
 * @property string $createdDate
 * @property string $updatedDate
 * @property string $lastVisitDate
 * @property string $lastActiveTime
 * @property string $avatarUrl
 * @property integer $isSuper
 * @property integer $isDeleted
 */
class User extends BaseDateModel {
    const SEX_MALE = 1;
    const SEX_FEMALE = 0;
    
    const MESSAGE_SUBSCRIBED = 1;
    const MESSAGE_UNSUBSCRIBED = 0;

    public $file_upload;

    /**
     * Returns the static model of the specified AR class.
     * @return User the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'pjm_user';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, password, email, address', 'required'),
            array('file_upload', 'file', 'types'=>'jpg, gif, png', 'allowEmpty' => true),//validate image type
            array('dateOfBirth', 'date', 'format'=>'yyyy-M-d'),
            array('sex, messageSubscribed, isDeleted', 'numerical', 'integerOnly' => true),
            array('name, password, email, address, avatarUrl', 'length', 'max' => 255),
            array('createdDate, updatedDate, lastVisitDate', 'safe'),
            array('email', 'email'),//validate email
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('userId, name, password, email, sex, dateOfBirth, address, messageSubscribed, createdDate, updatedDate, lastVisitDate, avatarUrl, isSuper, isDeleted', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'userId' => 'User',
            'name' => 'Name',
            'password' => 'Password',
            'email' => 'Email',
            'sex' => 'Sex',
            'dateOfBirth' => 'Date Of Birth',
            'address' => 'Address',
            'messageSubscribed' => 'Message Subsribed',
            'createdDate' => 'Created Date',
            'updatedDate' => 'Updated Date',
            'lastVisitDate' => 'Last Visit Date',
            'lastActiveTime' => 'Last Active Time',
            'avatarUrl' => 'Avatar Url',
            'isSuper' => 'Is Super',
            'isDeleted' => 'Is Deleted',
            'file_upload' => 'Avatar',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('userId', $this->userId, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('sex', $this->sex);
        $criteria->compare('dateOfBirth', $this->dateOfBirth, true);
        $criteria->compare('address', $this->address, true);
        $criteria->compare('messageSubscribed', $this->messageSubscribed);
        $criteria->compare('createdDate', $this->createdDate, true);
        $criteria->compare('updatedDate', $this->updatedDate, true);
        $criteria->compare('lastVisitDate', $this->lastVisitDate, true);
//        $criteria->compare('lastActiveTime', $this->lastActiveTime, true);
        $criteria->compare('avatarUrl', $this->avatarUrl, true);
        $criteria->compare('isSuper', $this->isSuper);
        $criteria->compare('isDeleted', $this->isDeleted);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }
    
    public function searchIsTrashed($isTrashed) {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('userId', $this->userId, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('sex', $this->sex);
        $criteria->compare('dateOfBirth', $this->dateOfBirth, true);
        $criteria->compare('address', $this->address, true);
        $criteria->compare('messageSubscribed', $this->messageSubscribed);
        $criteria->compare('createdDate', $this->createdDate, true);
        $criteria->compare('updatedDate', $this->updatedDate, true);
        $criteria->compare('lastVisitDate', $this->lastVisitDate, true);
//        $criteria->compare('lastActiveTime', $this->lastActiveTime, true);
        $criteria->compare('avatarUrl', $this->avatarUrl, true);
        $criteria->compare('isSuper', $this->isSuper);
        $criteria->compare('isDeleted', $isTrashed);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                    'pagination'=>array(
                        'pageSize'=>2,
                ),
                ));
    }

    protected function afterValidate() {
        parent::afterValidate();
        $this->password = $this->encrypt($this->password);
    }

    public function encrypt($value) {
        return md5($value);
    }
    
    public static function getSexOptions(){
        return array(
            self::SEX_FEMALE => 'Female',
            self::SEX_MALE => 'Male'
        );
    }
    
    public function getSexText(){
        $sexOptions = self::getSexOptions();
        return isset($sexOptions[$this->sex]) ? $sexOptions[$this->sex] 
                : "Unknown sex ({$this->sex})";
    }
    
    public static function getMessageSubscribedOptions(){
        return array(
            self::MESSAGE_UNSUBSCRIBED => 'Unsubsribed',
            self::MESSAGE_SUBSCRIBED  => 'Subscribed'
        );
    }

    public function getMessageSubscribedText(){
        $sexOptions = self::getMessageSubscribedOptions();
        return isset($sexOptions[$this->messageSubscribed]) ? $sexOptions[$this->messageSubscribed] 
                : "Unknown Message Subscribe Type ({$this->messageSubscribed})";
    }
}